//
//  SreenRankView.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/24.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "SreenRankView.h"

@implementation SreenRankView

-(instancetype)initWithTitle:(NSString *)title titleArr:(NSArray *)titleArr idArr:(NSArray *)idArr andFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(InfoNotificationAction) name:@"isReset" object:nil];
        self.frame = frame;
        self.title = title;
        //self.isReset=NO;
        self.reslutArray=[[NSMutableArray alloc]init];
        self.rankArray = [NSArray arrayWithArray:titleArr];
        self.rankIDArray=[NSArray arrayWithArray:idArr];
        [self rankView];
    }
    return self;
}


-(void)rankView{
    self.packView = [[UIView alloc] initWithFrame:self.frame];
    self.packView.y = 0;
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, MainW, 1)];
    line.backgroundColor = Gray_Color;
    [self.packView addSubview:line];
    UILabel *titleLB = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, MainW, 25)];
    titleLB.text = self.title;
    titleLB.font = FONT(14);
    titleLB.textColor=TextColor3;
    [self.packView addSubview:titleLB];
    
    self.btnView = [[UIView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(titleLB.frame), MainW, 40)];
    [self.packView addSubview:self.btnView];
    
    int count = 0;
    float btnWidth = 0;
    float viewHeight = 0;
    for (int i = 0; i < self.rankArray.count; i++) {
        
        NSString *btnName = self.rankArray[i];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundColor:SeparatorColor];
        [btn setTitleColor:TextColor3 forState:UIControlStateNormal];
        [btn setTitleColor:button_Red_Color forState:UIControlStateSelected];
        btn.titleLabel.font = FontAndStyle12;
        [btn setTitle:btnName forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        ViewRadius(btn, 3);
        CGSize btnSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font }];
        
        btn.width = btnSize.width + 25;
        btn.height = btnSize.height + 8;
        
        if (i==0)
        {
            btn.x = 10;
            btnWidth = CGRectGetMaxX(btn.frame);
        }
        else{
            btnWidth += CGRectGetMaxX(btn.frame)+10;
            if (btnWidth > MainW-120) {
            count++;
            btn.x = 10;
            btnWidth = CGRectGetMaxX(btn.frame);
            }else
            {
            btn.x += btnWidth - btn.bsg_width;
            }
        }
        btn.y += count * (btn.bsg_height+10)+10;
        
        viewHeight = CGRectGetMaxY(btn.frame)+10;
        
        [self.btnView addSubview:btn];
        btn.tag=[self.rankIDArray[i] intValue];
        //btn.tag = 1000+i;
    }
    self.btnView.bsg_height = viewHeight;
    self.packView.height = self.btnView.bsg_height+CGRectGetMaxY(titleLB.frame);
    self.height = self.packView.bsg_height;
    
    [self addSubview:self.packView];
}


-(void)btnClick:(UIButton *)sender{
    if ([self.isReset isEqualToString:@"是"]) {
        [self.reslutArray removeAllObjects];
        self.isReset=@"否";
    }
    sender.selected=!sender.selected;
    if (sender.selected) {
        sender.backgroundColor = SeparatorColor;
        sender.backgroundColor = [UIColor whiteColor];
        //self.selectBtn.selected = NO;
        sender.layer.borderWidth=0.5;
        sender.layer.borderColor=button_Red_Color.CGColor;
        if (![self.reslutArray containsObject:[NSString stringWithFormat:@"%ld",(long)sender.tag]]) {
            [self.reslutArray addObject:[NSString stringWithFormat:@"%ld",(long)sender.tag]];
        }

       
    }else{
        sender.backgroundColor = SeparatorColor;
        sender.layer.borderWidth=0.5;
        sender.layer.borderColor=[UIColor whiteColor].CGColor;
        [self.reslutArray removeObject:[NSString stringWithFormat:@"%ld",(long)sender.tag]];
       
    }
    if ([self.delegate respondsToSelector:@selector(selectBtnTitle:andBtn:andArray:)]) {
        
        [self.delegate selectBtnTitle:sender.titleLabel.text andBtn:sender andArray:self.reslutArray];
    }
}
-(void)InfoNotificationAction{
    self.isReset=@"是";
}
-(void)setBtnstatue:(UIButton *)btn{
    
}
@end
